package com.xzsoft.gdwz.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.UUID;

import org.joda.time.DateTime;

import com.xzsoft.gdwz.common.HttpClientUtil;
import com.xzsoft.gdwz.util.MD5;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class Test {
	public static void main(String[] args) throws IOException{
//		String loginUrl2 = "http://219.142.82.140:8090/gdxj/platformAction.do?method=personalLogin&loginName=WZJH-CY-HCFD&password="+HttpClientUtil.MD5("123456");
//		String url = "http://localhost:8080/xzda-web/xzda/example/"+UriEncoder.encode("core-site.xml");
//		String url1 = "http://localhost:8080/xzda-web/dataTable/exportDataTable.do?dataTableId="+"21aa1375-c66a-44d5-9de0-314a21fd2971"+"&dataTableName="+UriEncoder.encode("非烟用物资采购月度分类执行表");
//		String url2 = "http://219.142.82.140:8090/gdxj/xipAttMrgAction.do?method=down&selectedIds=bd9b7513-8191-468a-b325-08defac01c45";
//		String filepath = "E:/download/11/";
//		
//		//文件下载
//		HttpClientUtil.download(loginUrl2,url2,filepath);
//		//文件上传
//		String uploadUrl = "http://219.142.82.140:8090/gdxj/xipAttMrgAction.do?method=upload&src_id=3B586C9473BB6D5CE0534D00520A1892&attCatCode=JHGLFJ_JS&bus_no=JHGLFJ_JS&userId=IMP_USER_JH_0402";
//		HttpClientUtil.uploadFile(loginUrl2, uploadUrl, "IMG_3191.JPG", "E:/");
		
		String url = "http://192.168.1.135:8080/e-commerce-inf/ccidController/checkConstructionAndService";
		Map map = new HashMap();
		map.put("data", "{\"comId\":\"国电汉川发电有限公司\",\"createdBy\":\"WZJH-CY-HCFD\",\"companyDeptId\":\"安生部\",\"demandDp\":\"总经办\",\"reqPlanSort\":\"服务类\",\"projectType\":\"水电\",\"mtType\":\"其他类\",\"reqPlanType\":\"季度计划\",\"zhicaiFs\":\"\",\"inqAmount\":0,\"attribute10\":\"\",\"attribute3\":\"2017-05-03\",\"emergencyReason\":\"\",\"prMonth\":\"2017-04\",\"budAmount\":\"6\";\"reqDate\":\"2017-05-04\",\"reqContact\":\"张三\",\"reqContactTel\":\"010-52525252\",\"reqContactPhone\":\"13030303033\",\"reqCreatePerson\":\"李四\",\"reqCreatePersonTel\":\"010-42424242\",\"reqCreatePersonPhone\":\"15050505050\",\"costSource\":\"修理费：A修标准\",\"reviewMethod\":\"\",\"mtCode\":\"8888\",\"mtName\":\"ceshi0503\",\"mtDesc\":\"\",\"payWay\":\"货到验收合格后付款95%,质保金5%\",\"payMethod\":\"电汇\"}");
		String str = HttpClientUtil.httpPost(url, map);
		System.out.println(str);
	}
	
	public static String  getToken(){
		String username = "deli";
		String password = "1FA7A9DEBE973B2C8666958C0D1F09CF";
		DateTime dateTime = new DateTime();
		String timestamp = dateTime.toString("yyyy-MM-dd HH:mm:ss");
		String sign = MD5.GetMD5Code(username+timestamp+password).toUpperCase();
		Map<String,String> map = new HashMap<String,String>();
		String url = "http://219.142.82.141/oauth/accessToken";
		map.put("username", username);
		map.put("password", password);
		map.put("timestamp", timestamp);
		map.put("sign", sign);
		String result = HttpClientUtil.httpPost(url, map);
		JSONObject obj = JSONObject.fromObject(result);
		JSONObject re = JSONObject.fromObject(obj.get("result"));
		System.out.println("*************:"+result);
		return (String)re.get("access_token");
	}
	
	public static void deliverOrder(String token) throws IOException{
		DateTime dateTime = new DateTime();
		String createdate = dateTime.toString("yyyy-MM-dd HH:mm:ss");
		List list = new ArrayList();
		for(int j = 0 ; j<2 ;j++){
			String orderId = UUID.randomUUID().toString();
			Map map = new HashMap();
			map.put("ADDRESS", "北京市昌平区北七家镇");
			map.put("CREATE_BY", "Gdcyhcdyfd@qq.com");
			map.put("CREATE_BY_PHONE", "13808684178");
			map.put("CREATE_DATE", createdate);
			map.put("INQ_SOURCE", "DL");
			map.put("ECOMMERCE_ORDER_ID", "orderId");
			map.put("INQ_AMOUNT", 1000);
			List lines = new ArrayList();
			for(int i=0;i<3;i++){
				Map line = new HashMap();
				line.put("ECOMMERCE_SKU_ID", UUID.randomUUID().toString());
				line.put("MT_NAME", "商品"+getRandomString(10));
				line.put("MT_CODE", getRandomString(10));
				line.put("REQ_BRAND", getRandomString(8));
				line.put("REQ_PRICE", getRandomInt(50,200)+"");						
				line.put("REQ_QUANTITY", getRandomInt(1,10)+"");
				line.put("CREATION_DATE", createdate);
				line.put("CREATED_BY", "Gdcyhcdyfd@qq.com");
				line.put("REQ_SPEC", getRandomString(20));
				line.put("REQ_MATERIAL", getRandomString(20));
				line.put("REQ_BRAND", getRandomString(15));
				line.put("REQ_MATERIAL", getRandomString(12));
				line.put("REQ_UNIT", "个");
				line.put("REQ_MODEL", getRandomString(12));
				line.put("PR_NAME", getRandomString(7));
				lines.add(line);
			}
			map.put("ORDER_LINES", lines);
			list.add(map);
		}
		JSONArray array  = JSONArray.fromObject(list);
		Map map = new HashMap();
		map.put("token", token);
		String orderInfo="[{\"ADDRESS\":\"吉林白城市白城市10010010101\",\"COM_ID\":\"CGDCCompanyId10\",\"COM_NAME\":\"10010010101\",\"CREATE_BY\":\"Gdcyhcdyfd@qq.com\",\"CREATE_BY_PHONE\":\"13376251623 \",\"CREATE_DATE\":\"2016-08-10 14:28:57\",\"INQ_AMOUNT\":3800.00,\"INQ_H_CODE\":\"24008856\",\"INQ_H_ID\":\"24008856\",\"INQ_H_NAME\":\"\",\"INQ_SOURCE\":\"DL\",\"ORDER_LINES\":[{\"ADDRESS\":\"吉林白城市白城市10010010101\",\"CCID_ID\":\"1\",\"INQ_H_ID\":\"24008856\",\"MT_CODE\":\"100038882\",\"MT_NAME\":\"海尔BCD-521WDPW电冰箱(台)\",\"REQ_AMOUNT\":3800.00,\"REQ_BRAND\":\"海尔\",\"REQ_MATERIAL\":\"\",\"REQ_MODEL\":\"\",\"REQ_PRICE\":3800.00,\"REQ_PRODUCER\":\"\",\"REQ_QUANTITY\":1,\"REQ_SPEC\":\"\",\"REQ_UNIT\":\"\"}],\"REQ_DATE\":\"\",\"VENDOR_NAME\":\"\",\"VENDOR_PHONE\":\"\"}]";
		map.put("order_info", orderInfo);
		System.out.println(array.toString());
		String url = "http://219.142.82.141/order/reciveOrder";
		//String url = "http://localhost/e-commerce-inf/order/reciveOrder";
		String result = HttpClientUtil.httpPost(url, map);
		System.out.println(result);
	}
	
	public static String getRandomString(int length) { //length表示生成字符串的长度
	    String base = "abcdefghijklmnopqrstuvwxyz0123456789";   
	    Random random = new Random();   
	    StringBuffer sb = new StringBuffer();   
	    for (int i = 0; i < length; i++) {   
	        int number = random.nextInt(base.length());   
	        sb.append(base.charAt(number));   
	    }   
	    return sb.toString();   
	 }   
		
	 public static int getRandomInt(int max,int min){		
        Random random = new Random();
        int s = random.nextInt(max)%(max-min+1) + min;
        return s;
	 }
	 
}
